﻿#include "QQ_Login.h"
#include <QMovie>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include "eventfilterobject.h"
#include <QGraphicsDropShadowEffect>

#include "QApp.h"
#define Config QApp::configs()
QString QQ_Login::m_Token = {};
QQ_Login::QQ_Login(QDialog* parent)
	:QDialog(parent)
	,ui(new Ui::Form)
	,m_manager(new QNetworkAccessManager)
{
	ui->setupUi(this);
	this->initUi();
}

void QQ_Login::initUi()
{
	setWindowFlag(Qt::WindowType::FramelessWindowHint);
	setWindowFlag(Qt::WindowType::WindowStaysOnTopHint);
	setAttribute(Qt::WA_TranslucentBackground);
	installEventFilter(new eventFilterObject(this));

	auto movie = new QMovie("../images/gifLabel.gif");
	ui->topLab->setMovie(movie);
	movie->start();

	ui->usernameCmb->lineEdit()->addAction(QIcon("../images/username.png"), QLineEdit::LeadingPosition);
	ui->passwordEdit->addAction(QIcon("../images/passwd.png"), QLineEdit::LeadingPosition);

	//添加边框阴影
	auto effect = new QGraphicsDropShadowEffect(this);
	effect->setColor(qRgb(200, 200, 200));
	effect->setBlurRadius(15);
	effect->setOffset(0);
	this->setGraphicsEffect(effect);

	ui->profileLab->setPixmap(QPixmap("../images/portrait/default.jpg"));
	ui->profileLab->setScaledContents(true);
	ui->profileLab->setMask(QRegion(0, 0, 68, 68, QRegion::RegionType::Ellipse));

	effect = new QGraphicsDropShadowEffect(this);
	effect->setColor(qRgb(200, 200, 200));
	effect->setBlurRadius(15);
	effect->setOffset(0);
	ui->profileLab->setGraphicsEffect(effect);
	LoadFromConfig();
}

void QQ_Login::SaveToConfig()
{
	Config->setValue("username", ui->usernameCmb->currentText());
	Config->setValue("password", ui->passwordEdit->text());
	Config->setValue("autoLogin", ui->autoLoginCbx->isChecked());
	Config->setValue("remerberPassword", ui->rememberCbx->isChecked());
}

void QQ_Login::LoadFromConfig()
{
	ui->usernameCmb->setCurrentText(Config->value("username").toString());
	ui->passwordEdit->setText(Config->value("password").toString());
	ui->autoLoginCbx->setChecked(Config->value("autoLogin").toBool());
	ui->rememberCbx->setChecked(Config->value("remerberPassword").toBool());
}

void QQ_Login::on_minBtn_released()
{
	this->showMinimized();
	qInfo("%s", __FUNCTION__);
}

void QQ_Login::on_closeBtn_released()
{
	SaveToConfig();
	this->close();
	qInfo("%s", __FUNCTION__);
}

void QQ_Login::on_loginBtn_released()
{
	auto email = ui->usernameCmb->currentText();
	auto password = ui->passwordEdit->text();

	QUrl url("http://" + Config->value("Host/host").toString() + "/login");
	QNetworkRequest request(url);
	request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
	QJsonDocument requestBodyJdom(QJsonObject{
		{"email",email},
		{"password",password}
		});
	QNetworkReply* reply = m_manager->post(request, requestBodyJdom.toJson(QJsonDocument::Compact));
	connect(reply, &QNetworkReply::errorOccurred, [=]()
		{
			qDebug() << "QNetworkReply::errorOccurred";
		});
	connect(reply, &QNetworkReply::finished, [=]()
		{
			if (reply->error() == QNetworkReply::NetworkError::NoError)
			{
				//auto data = reply->readAll();
				QJsonDocument replyJdom = QJsonDocument::fromJson(reply->readAll());
				//qInfo() << data;
				qDebug() << replyJdom;
				if (!replyJdom["Token"].toString().isEmpty())
				{
					m_Token = replyJdom["Token"].toString();
					accept();
				}
			}
			else
			{
				qDebug() << "QNetworkReply::NetworkError::" << reply->error();
			}
			reply->deleteLater();	
		});
	//if (!username.isEmpty() && !password.isEmpty())
	//{
	//	auto mainWindow = new MainWindow;
	//	mainWindow->setAttribute(Qt::WA_DeleteOnClose);
	//	mainWindow->show();
	//	this->close();
	//}

	//qInfo("%s %s %s", __FUNCTION__, username.toStdString().c_str(), password.toStdString().c_str());
}

void QQ_Login::on_autoLoginCbx_clicked()
{
	if (ui->autoLoginCbx->isChecked())
		ui->rememberCbx->setChecked(true);
}

void QQ_Login::resizeEvent(QResizeEvent* ev)
{
	ui->profileLab->move((this->width() - ui->profileLab->width())/2, 89);
}
